package mypack.srverlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import mypack.bean.Counter;

public class CounterServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		doPost(request, response);
	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		
		ServletContext context = getServletContext();
		
        Counter count = (Counter)context.getAttribute("counter");
        if(null==count){
        	count = new Counter(1);
        	context.setAttribute("counter", count);	
        } else {
        	count.addCount(); 
        }
        System.out.println(request.getRequestURI());
        System.out.println(request.getQueryString());
        System.out.println(request.getContextPath());
        System.out.println(request.getMethod());
        response.setContentType("text/html;charset=GB2312");
        response.setHeader("Exprise", "0");
        int number = count.getCount();
        try {
			PrintWriter writer = response.getWriter();
			writer.write("<html><body>");
			writer.write("访问次数==="+number);
			writer.write("</body></html>");
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
        
        
        
	}

}
